package com.beikai.springbootdemo.config.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

/**
 * @ClassName TransactionManagementConfiguration
 * @Description TODO
 * @Author Admin
 * @Date 2018/11/20 0:54
 * @Version 1.0
 **/
/**
 * 对标spring-service里面的transactionManager
 * 继承TransactionManagementConfigurer是因为开启annotation-driven
 *
 * @author xiangze
 *
 */
@Configuration
/**
 * 首先使用注解 @EnableTransactionManagement 开启事务支持后
 * 在Service方法上添加注解 @Transactional 便可
 */

@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {

    /**
     * 注入DataSourceConfiguration里边的dataSource,通过createDataSource()获取
     */
    @Autowired
    private DataSource dataSource;
    /**
     * 关于事务管理，需要返回PlatformTransactionManager的实现
     */
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}
